<?php
/*
 * 手机客户端 秀场作品评论接口
 * 传入参数 
 * 2013-12-011
 */
include "inc/public_header.php";
 
include "func/user.php";				//用户信息处理函数

$reJson = array();//构建返回值
 
$page			= $_REQUEST["page"]; 				//页数，第一页为0
$pagesize		= $_REQUEST["pagesize"];			//每页记录数
$zpid			= $_REQUEST["worksid"];				//作品id
$acontent 		= $_REQUEST["content"];			    //评论
$pingfen 		= $_REQUEST["level"];				//评分
$hd_id			= $_REQUEST["hd_id"];				//活动主键
$vcode			= $_REQUEST["vcode"];				//图片验证码

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array("success"=>"0","errCode"=>"0"));
	die;
}
$uid = $uInfoArr["uid"];//用户信息主键

//判断验证码
$sql = "select avalue from user_phone_imgvcode where uuid='".$_REQUEST["uuid"]."' and away='1'";
$db->query($sql);
if(!(isset($vcode) && $vcode!="" && $db->next_record() && $vcode==$db->f("avalue"))){
	echo json_encode(array("success"=>"0","errCode"=>"2","msg"=>"验证码错误"));
	die;
}

//自己不能给自己评论
$sql = "select id from new_weituo_zuopin where id='".$zpid."' and uid='".$uid."'";
$db->query($sql);
if($db->next_record()){
	$reJson["success"] = "0";
	$reJson["errCode"] = "1";
	echo json_encode($reJson);
	die;
}else{
	$reJson["success"] = "1";
}

//获取以前评分记录
$sql = "select id from new_weituo_pingfen where zpid='".$zpid."' and uid='".$uid."'";
$db->query($sql);

if($db->next_record()){
	//更新评价信息
	$sql = "update new_weituo_pingfen set pingfen='".$pingfen."' where id='".$db->f("id")."'";
}else{
	$sql = "insert into new_weituo_pingfen (uid,zpid,pingfen) values('".$uid."','".$zpid."','".$pingfen."')";
}
$db->query($sql); //执行更新


//获取平均分和参与人数,赞人数
$sql = "select (sum(pingfen)/count(id)) as fen,count(id) as ct from new_weituo_pingfen where zpid='".$zpid."' and pingfen>0";
$db->query($sql);
if($db->next_record()){
	//更新作品分数
	$sql = "update new_weituo_zuopin set pingfen='".$db->f("fen")."',pf_count='".$db->f("ct")."' where id='".$zpid."'";
	$db->query($sql);
}

$sql = "insert into new_weituo_comment (uid,createtime,comment,zpid,from_type) values('".$uid."',now(),'".$acontent."','".$zpid."','1')";
$db->query($sql);//提交评论

$sql = "SELECT count(*) as count from  new_weituo_comment where zpid in (SELECT id from new_weituo_zuopin where uid=(SELECT uid from new_weituo_zuopin where id='".$zpid."' and dr=0 and aflag=1)) and DATE(createtime)=CURDATE()";
$db->query($sql);
if($db->next_record()){
	$count = $db->f('count');
	if ($count == 1){
		$x_sql="SELECT b.mobile from new_weituo_zuopin a INNER JOIN user_base b on a.uid=b.uid where a.id='".$zpid."'";
		$db2->query($x_sql);
		if($db2->next_record())
		{
			sendMessage($db2->f("mobile"),"同学你好，你的作品有人评论咯，请记得去回复一下哦");
		}
	}
}

echo json_encode($reJson);

function sendMessage($mobile, $message) 
{ 
	/* 通过企信通的HTTP接口*/
	$ip = "121.101.221.34";
	$port = "8888";
	$x_url = "http://121.101.221.34:8888/sms.aspx";
	$xx = "action=send&userid=1564&account=jkcs45&password=jkcs45888&mobile=" . $mobile . "&content=" . $message . "";
    

	
	$fp = fsockopen($ip, $port, $errno, $errstr, 10);
	$reply = "";
	if ($fp) {
		fputs($fp, "POST " . $x_url . " HTTP/1.1\r\n");
		fputs($fp, "Host: " . $ip . "\r\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: " . strlen($xx) . "\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $xx . "\r\n\r\n");

		$reply = "";
		while (!feof($fp)) {
			$reply .= fgets($fp, 4096);
		}

		
	}
	$reply_char=explode("Connection: close",$reply);
	$result=explode("-",trim($reply_char[1]));
	if(trim($result[0])=="DELIVRD"){
		return "00";
	}else{
		return "01";
	}

}
?>